/*
 * Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
 *
 * Licensed under the Mulan PSL v2.
 * You can use this file according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of the License at
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
 * FOR MORE DETAILS.
 */

#pragma once
#include "highfive/H5File.hpp"

#include "DBManager/IRepository/Input/Dynamic/IEIGRLRepository.h"
#include "DBManager/Repository/HDF5/Base/SetRepositoryBase.h"

SG_DBMANAGER_HDF5_NAMESPACE_OPEN
namespace EIGRL
{
    struct IDENTITY
    {
        SG::DataStructure::Common::I64  m_SID = 0.0;
        SG::DataStructure::Common::Real m_V1 = 0.0;  ///< 求解下限，闭区间，见remark 2、4，(Real or blank, – 5*10^16<= V1 <= V2 <=5.*10^16 )
        SG::DataStructure::Common::Real m_V2 = 0.0;  ///< 求解上限，闭区间，见remark 2、4，(Real or blank, – 5*10^16<= V1 <= V2 <=5.*10^16 )
        SG::DataStructure::Common::I64  m_ND = 0;    ///< 求解阶数, (Integer > 0 or blank)
        SG::DataStructure::Common::I64  m_MSGLVL  = 0;  ///< 控制在特征值提取过程中诊断输出的数量，见remark 7((0 < Integer < 4; Default = 0)
        SG::DataStructure::Common::I64  m_MAXSET  = 0;  ///< 在block或set中的向量个数，默认与机器相关，见remark 8
        SG::DataStructure::Common::Real m_SHFSCL  = 0.0;    ///< 预估的第一阶非刚体模态，见remark 9  (Real or blank)
        SG::DataStructure::Common::I64  m_FLAG1   = 0;      ///< -1代表v1为空，非-1代表为非空
        SG::DataStructure::Common::I64  m_FLAG2   = 0;      ///< -1代表v2为空，非-1代表为非空
        SG::DataStructure::Common::I8   m_NORM[4] = { 0 };  ///< 归一化方法 (Character: “MASS” or “MAX”)
        SG::DataStructure::Common::Real m_ALPH    = 0.0;    ///< (Real > 0.0; Default = 1.0):（实测HDF5为0）
        SG::DataStructure::Common::I64  m_FREQS_POS = 0;    ///<
        SG::DataStructure::Common::I64  m_FREQS_LEN = 0;    ///<
        SG::DataStructure::Common::I64  m_DOMAIN_ID = 0;    ///<
    };
};  // namespace EIGRL
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::EIGRL::IDENTITY> ()
{
    HighFive::CompoundType compoundType = {
        { "SID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "V1", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "V2", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "ND", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "MSGLVL", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "MAXSET", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "SHFSCL", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "FLAG1", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "FLAG2", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "NORM", HighFive::create_datatype<char[4]> (), 1, true },
        { "ALPH", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "FREQS_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "FREQS_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "DOMAIN_ID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
    };
    return compoundType;
}
SG_DBMANAGER_HDF5_NAMESPACE_OPEN
class EIGRLRepository : public IEIGRLRepository, public SetRepositoryBase<SG::DataStructure::FEM::EIGRLData>
{
  public:
    explicit EIGRLRepository (HDF5Database& database);

    enum DATASET : std::uint8_t
    {
        MainDataset,   ///< 主表
        IndexDataset,  ///< 主键表
        SubDataset     ///< FI表单
    };

    /**
     * @brief   : 初始化表数据
     * @author  : wxc
     */
    void InitialDatasetInfo () override;

    /**
     * @brief   : 将缓存数据转储到文件内
     */
    void DumpWriteCahce () override;

    /**
     * @brief   : 加载文件数据至缓存
     * @param   : loadIndex
     */
    std::vector<DataType>* LoadReadCache (std::size_t loadIndex) override;

    /**
     * @brief   : 对数据进行排序
     */
    void SortData () override;

    /**
     * @brief   : 根据sid查找数据
     * @param   : sid
     * @param   : arrs
     * @return  :
     */
    SG::DataStructure::Common::Status FindBySId (SG::DataStructure::Common::Id                        sid,
                                                 std::vector<SG::DataStructure::FEM::EIGRLData>& _OUT arrs) override;

    /**
     * @brief   : 将自己的数据转存为nastran的数据结构
     */
    void OwnToNastran (std::vector<SG::DataStructure::FEM::EIGRLData>&         own,
                       std::vector<SG::DBManager::HDF5::EIGRL::IDENTITY>& _OUT IDENTITYData,
                       std::vector<SG::DataStructure::Common::I64>& _OUT       FREQSData,
                       bool                                                    isSort = false);

    /**
     * @brief   : 将NASTRAN的数据转存为自己的数据结构
     * @param   : IDENTITYData
     * @param   : FREQSData
     * @param   : own
     */
    void NastranToOwn (std::vector<SG::DBManager::HDF5::EIGRL::IDENTITY>&   IDENTITYData,
                       std::vector<SG::DataStructure::Common::I64>&         FREQSData,
                       std::vector<SG::DataStructure::FEM::EIGRLData>& _OUT own);
};
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
